package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"jujiu/biz"
	"jujiu/biz/dts"
	"log"
	"strconv"
	"strings"
)

type DispatchController struct {
	beego.Controller
}

func (d *DispatchController) CommonDispatch() {
	// 获取参数
	dReq := &struct {
		DeviceId string `json:"deviceId"`
		SCid     string `json:"sCid"`
		TCid     string `json:"tCid"`
	}{}
	if err := json.Unmarshal(d.Ctx.Input.RequestBody, dReq); err != nil {
		d.Ctx.WriteString("请求参数解析失败")
		return
	}
	if strings.HasPrefix(dReq.DeviceId, "86") {
		id := biz.Imei2DeviceId(dReq.DeviceId, dReq.SCid);
		dReq.DeviceId = strconv.FormatInt(id, 10)
	} else if len(dReq.DeviceId) == 4 {
		id := biz.DeviceNo2DeviceId(dReq.DeviceId, dReq.SCid);
		dReq.DeviceId = strconv.FormatInt(id, 10)
	}
	deviceId, _ := strconv.ParseInt(dReq.DeviceId, 10, 64)
	// 判断设备是否在线
	online := biz.CheckDeviceOnline(deviceId, dReq.SCid)
	if !online {
		d.Ctx.WriteString("迁移校验失败（设备不在线）")
		return
	}
	// 1、确定设备激活集群
	ActiveCid := biz.GetActiveCid(deviceId, dReq.SCid)
	// 2、同步数据到目标连接集群
	if strings.HasSuffix(dReq.TCid, "dev") {
		dts.Syn2OrtherDatasource(deviceId, dReq.SCid, dReq.TCid)
	}
	// 3、修改激活集群中的设备的目标连接地址
	biz.UpdateTcid(deviceId, ActiveCid, dReq.TCid)
	// 4、获取设备当前连接的地址
	// 5、下发指令
	result, desc := biz.Dispatch(deviceId, dReq.TCid, dReq.SCid)
	if result.Success {
		d.Ctx.WriteString("success")
	} else {
		d.Ctx.WriteString(desc + result.Msg)
	}
	return
}

/**
国内正式到测试
 */
func (d *DispatchController) CNReal2CNdev() {
	deviceId, e := d.GetInt64("deviceId")
	if e != nil {
		log.Println("获取参数错误！")
		return
	}
	b, s := dts.Syn2OrtherDatasource(deviceId, "cn_real", "cn_dev")
	if !b {
		d.Ctx.WriteString("数据迁移失败," + s)
		return
	}

	result, desc := biz.Dispatch(deviceId, "cn_dev", "cn_real")

	if result.Success {
		d.Ctx.WriteString("success")
	} else {
		d.Ctx.WriteString(desc + result.Msg)
	}
	return
}

/**
国内正式到海外
 */
func (d *DispatchController) CNReal2EUReal() {
	deviceId, e := d.GetInt64("deviceId")
	if e != nil {
		log.Println("获取参数错误！")
		return
	}

	b, s := dts.Syn2OrtherDatasource(deviceId, "cn_real", "eu_real")
	if !b {
		d.Ctx.WriteString("数据迁移失败," + s)
		return
	}

	result, desc := biz.Dispatch(deviceId, "eu_real", "cn_real")

	if result.Success {
		d.Ctx.WriteString("success")
	} else {
		d.Ctx.WriteString(desc + result.Msg)
	}
	return
}

/**
海外到国内正式
 */
func (d *DispatchController) EUReal2CNReal() {
	deviceId, e := d.GetInt64("deviceId")
	if e != nil {
		log.Println("获取参数错误！")
		return
	}
	b, s := dts.ResetDeviceCid2CNReal(deviceId, "Hangzhou")
	if !b {
		d.Ctx.WriteString("数据设置失败," + s)
		return
	}
	result, desc := biz.Dispatch(deviceId, "cn_real", "eu_real")

	if result.Success {
		d.Ctx.WriteString("success")
	} else {
		d.Ctx.WriteString(desc + result.Msg)
	}
	return
}

/**
国内测试到国内正式
 */
func (d *DispatchController) CNDev2CNReal() {
	deviceId, e := d.GetInt64("deviceId")
	if e != nil {
		log.Println("获取参数错误！")
		return
	}

	b, s := dts.ResetDeviceCid2CNReal(deviceId, "Hangzhou")
	if !b {
		d.Ctx.WriteString("数据设置失败," + s)
		return
	}

	result, desc := biz.Dispatch(deviceId, "cn_real", "cn_dev")
	if result.Success {
		d.Ctx.WriteString("success")
	} else {
		d.Ctx.WriteString(desc + result.Msg)
	}
	return
}
